<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/11/011
 * Time: 8:57
 */

namespace app\project\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Account extends Base
{

    /**
     * 列表
     */
    public function index(){
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['name'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['mobile'])){
            $mobile = $_GET['mobile'];
            $where['mobile'] = ['like',"%{$mobile}%"];
        }
        if(!empty($_GET['phone'])){
            $phone = $_GET['phone'];
            $where['phone'] = ['like',"%{$phone}%"];
        }
        $common = new Common();
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $thisWechat = Session::get("thisWechat");
                $where['uniacid'] = $thisWechat['id'];
                $where['status'] = ['gt',0];
                $list = $common->public_select("project_account",$where,null)->limit($start,$limit)->order("id desc")->select();
                $count = $common->public_select("project_account",$where)->count();
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        return $this->fetch("index");
    }

    /**
     * 详情
     */
    public function detail(){
        $common = new Common();

        $thisWechat = Session::get("thisWechat");
        $typewhere['uniacid'] = $thisWechat['id'];
        //查询已有单位
        $dictionaries = $common->public_select("project_dictionaries",$typewhere,"id,name")->order("id desc")->select();
        $this->assign("dictionaries",$dictionaries);

        if(!empty($_GET['id'])){
            $where['id'] = $_GET['id'];
            $detail = $common->public_select("project_account",$where)->find();
            $detail['fans_name'] = '';
            if(!empty($detail['fans_id'])){
                $accountWhere['id'] = $detail['fans_id'];
                $detail['fans_name'] = Db::name("admin_fans")->where($accountWhere)->value("nickname");
            }
        }else{
            $queryTable = Db::query("show columns from us_project_account"); //获取表结构
            foreach ($queryTable as $item) {
                $detail[$item['Field']] = "";
            };
            $detail['fans_name'] = '';
        }
        $this->assign("detail",$detail);


        $template = "saveView";
        return $this->fetch($template);
    }

    /**
     * 添加操作
     */
    public function add(){
        $thisWechat = Session::get("thisWechat");
        $common = new Common();
        try{
            $rule = [
                ["name", "require", "名称不能为空"],
                ["mobile", "require", "账号不能为空"],
                ["password", "require", "密码不能为空"],
                ["password1", "require", "确认密码不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }

            //查询手机号是否存在
            $where['mobile'] = $data['mobile'];
            $where['uniacid'] = $thisWechat['id'];
            $detail = $common->public_select("project_account",$where)->value("id");
            if(!empty($detail)){
                throw new Exception("该账号已存在,请勿重复添加");
            }

            $this->condition($common,$data);
            unset($data['password1']);
            $data['password'] = md5($data['password']);
            $data['addtime'] = date("Y-m-d H:i:s",time());
            $data['add_id'] = Session::get("id");
            $id = $common->handle("project_account",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】添加新账号,账号id【" . $id . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 修改操作
     */
    public function save(){
        $thisWechat = Session::get("thisWechat");
        $common = new Common();
        try{
            $rule = [
                ["name", "require", "名称不能为空"],
                ["mobile", "require", "账号不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            $data['uniacid'] = $thisWechat['id'];
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            $this->condition($common,$data);

            //查询手机号是否存在
            $where['mobile'] = $data['mobile'];
            $where['uniacid'] = $thisWechat['id'];
            $where['id'] = ['neq',$data['id']];
            $detail = $common->public_select("project_account",$where)->value("id");
            if(!empty($detail)){
                throw new Exception("该账号已存在，请勿重复添加");
            }
            unset($data['password1']);
            if(empty($data['password'])){
                unset($data['password']);
            }else{
                $data['password'] = md5($data['password']);
            }

            $data['update_time'] = date("Y-m-d H:i:s",time());
            $data['update_id'] = Session::get("id");
            $common->handle("project_account",$data,1);
            $log['remark'] = "【工程管理】用户【" . Session::get("user_name") . "】更新账号,账号id【" . $data['id'] . "】";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "添加成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    /**
     * 删除操作
     */
    public function del(){
        try{
            $common = new Common();
            $id = $_POST['id'];

            $power = $common->public_select("project_account",['id'=>['in',$id]])->select();
            $res = $common->public_select("project_account",['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }

            foreach ($power as $k=>$v){
                $log['remark'] = "【工程管理】,账号【" . Session::get("user_name") . "】删除账号【".$v['name']."】id【".$v['id']."】";
                $common->addLog($log);
            }
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    public function condition($common,$data){
//        if(!$common->isMobile($data['mobile'])){
//            throw new Exception("手机号格式有误");
//        }
        if(!empty($data['password'])){
            if(strlen($data['password'])<6 | strlen($data['password'])>20){
                throw new Exception("密码长度必须为6-20位字符");
            }
            if ($data['password'] != $data['password1']) {
                throw new Exception("两次输入的密码不一致");
            }
        }

    }

}